package kr.co.bettersoft.sendme.provider;

import java.util.ArrayList;

import kr.co.bettersoft.sendme.activity.R;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class SendMeReservationMessageGroupEditAdapter extends BaseAdapter {

	
	private Context context;
	private ArrayList<SendMeFriend> friendList;
	private SendMeFriend friend;
	LayoutInflater layoutInflater;
	
	
	String TAG = "SendMeReservationMessageGroupEditAdapter";
	
	View view;
	
	ImageButton delectFromGroupCheckBox;				 
	
	public SendMeReservationMessageGroupEditAdapter(Context context, ArrayList<SendMeFriend> friendList) {
		this.context = context;
		this.friendList = friendList;
		layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		if(friendList!=null){
			return friendList.size();
		}else{
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return friendList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		view = convertView;
		
		ViewWrapper wrapper = null;
		// 태그를 이용하여 position 값을 정확한 값을 사용하도록 함. 태그를 저장후 나중에 태그값을 꺼내어 사용
		if(view == null) {
			view = layoutInflater.inflate(R.layout.activity_send_me_reservation_message_list_group_edit_adapter, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}
		
		friend = friendList.get(position);
		
		delectFromGroupCheckBox = (ImageButton) view.findViewById(R.id.imageButtonSendCheck);
		delectFromGroupCheckBox.setTag(position);
		
		delectFromGroupCheckBox.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				int selectedPosition = Integer.parseInt(v.getTag()+"");
				Log.d(TAG,"selectedPosition:"+selectedPosition);
				if(friendList.get(selectedPosition).getIsSelectedForSend()==false){
					friendList.get(selectedPosition).setSelectedForSend(true);
					v.setBackgroundResource(R.drawable.blue_checkbox_on);
				}else{
					friendList.get(selectedPosition).setSelectedForSend(false);
					v.setBackgroundResource(R.drawable.blue_checkbox_off);
				}
			}
		});
		// 체크 여부에 따라 이미지를 세팅 한다.
		if(friend.getIsSelectedForSend()) {
			delectFromGroupCheckBox.setBackgroundResource(R.drawable.blue_checkbox_on);
		} else {
			delectFromGroupCheckBox.setBackgroundResource(R.drawable.blue_checkbox_off);
		}
		
		// 친구 이름을 세팅 한다.
		wrapper.getTxtViewFriendName().setText(friend.getName());
		
		// 설치 여부에 따라 이미지를 세팅 한다.
		if(("Y").equals(friend.getInstallYn())) {
			wrapper.getImgViewInstall().setVisibility(View.VISIBLE);		// 설치 사용자인지
		} else {
			wrapper.getImgViewInstall().setVisibility(View.INVISIBLE);
		}
		
		return view;
	}
	
	class ViewWrapper {
		View base;
		
		TextView txtViewFriendName;				// 친구 이름.
		ImageView imgViewInstall;				// 설치 여부.
		
		ViewWrapper(View base) {
			this.base = base;
		}
		
		TextView getTxtViewFriendName() {
			if(txtViewFriendName == null) {
				txtViewFriendName = (TextView)view.findViewById(R.id.txtViewFriendName);
			}
			
			return txtViewFriendName;
		}
		
		ImageView getImgViewInstall() {
			if(imgViewInstall == null) {
				imgViewInstall = (ImageView)view.findViewById(R.id.imgViewInstall);
			}
			
			return imgViewInstall;
		}
	}
	
}